/*
 * TrinomialTreeView.java
 */
package trinomialtree.gui;

import java.awt.Color;
import java.util.List;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import trinomialtree.input.RateInputReader;
import trinomialtree.math.PayOffNegativeException;
import trinomialtree.math.TrinomialTree;
import trinomialtree.treevisualizer.Visualizer;

/**
 * The application's main frame.
 */
public class TrinomialTreeView extends FrameView {

    public static final String RATE_INPUT = "C:\\Users\\eva\\Documents\\NetBeansProjects\\TrinomialTree\\input\\";

    public enum mode {YEAR, MONTH, WEEK};

    private TrinomialTree tree;

    private Visualizer visualizer;

    public TrinomialTreeView(SingleFrameApplication app) {
        super(app);

        initComponents();

        
        //Setze das Resultlabel
        this.jLabel7.setText("");

        this.showInfo("");

        //Setze zum Testen Default-Werte
        //this.jTextField1.setText("2");
        //this.jTextField6.setText("2");
        //this.jTextField7.setText("0.05");
        //jComboBox1.setSelectedIndex(3);

        try {

            //Hole die Datumswerte, für welche Zinsen verfügbar sind und zeige sie dem User an
            RateInputReader reader = new RateInputReader(RATE_INPUT);
            List availableDates = reader.readAvailableDates();
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(availableDates.toArray()));

        } catch (Exception e) {
            showError(e.getMessage());
        }



    }

    private void showInfo(String infoString) {
        this.statusMessageLabel.setForeground(Color.black);
        this.statusMessageLabel.setText(infoString);
    }

    private void showError(String errorString) {
        this.statusMessageLabel.setForeground(Color.red);
        this.statusMessageLabel.setText(errorString);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = TrinomialTreeApp.getApplication().getMainFrame();
            aboutBox = new TrinomialTreeAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        TrinomialTreeApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trinomialtree.gui.TrinomialTreeApp.class).getContext().getResourceMap(TrinomialTreeView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "European Put Bondoption", "European Call Bondoption", "European Put Swaption", "European Call Swaption" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                TrinomialTreeView.this.itemStateChanged(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField1.setText(resourceMap.getString("durationOption.text")); // NOI18N
        jTextField1.setName("durationOption"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hallo", "Du", " " }));
        jComboBox2.setName("referenzDatumsWerte"); // NOI18N
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                itemStateChangedDatum(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TrinomialTreeView.this.actionPerformed(evt);
            }
        });

        jLabel7.setFont(resourceMap.getFont("resultLabel.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("resultLabel.text")); // NOI18N
        jLabel7.setName("resultLabel"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setName("bondPanel"); // NOI18N

        jLabel9.setText(resourceMap.getString("Laufzeit Bond.text")); // NOI18N
        jLabel9.setName("Laufzeit Bond"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextField2.setText(resourceMap.getString("nominalBond.text")); // NOI18N
        jTextField2.setName("nominalBond"); // NOI18N

        jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jTextField3.setText(resourceMap.getString("strike.text")); // NOI18N
        jTextField3.setName("strike"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel9)
                    .addComponent(jLabel4))
                .addGap(43, 43, 43)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField3)
                    .addComponent(jTextField4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                .addContainerGap(161, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("bondPanel.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jPanel1.setName("swaptionPanel"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "4" }));
        jComboBox4.setName("jComboBox4"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel11))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))))
                .addContainerGap(155, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("swaptionPanel.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Wöchentlich", "Monatlich", "Jährlich" }));
        jComboBox3.setSelectedIndex(1);
        jComboBox3.setName("jComboBox3"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(63, 63, 63)
                        .addComponent(jLabel7))
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(64, 64, 64)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel12))
                                .addGap(18, 18, 18)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addComponent(jButton1))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(165, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGap(46, 46, 46)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(37, Short.MAX_VALUE))
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName(resourceMap.getString("jTabbedPane1.AccessibleContext.accessibleName")); // NOI18N

        menuBar.setName("menuBar"); // NOI18N

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 611, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void actionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionPerformed


        this.showInfo("Berechnung läuft...");

        Double result = 0d;


        //Wurde ein Laufzeit für die Option eingegeben
        Integer laufZeitOption = 0;
        if ((laufZeitOption = isInteger(this.jTextField1.getText())) == null) {
            showError("Bitte geben Sie eine Laufzeit für die Option an (nur ganze Zahlen).");
            return;
        }

        //Die Laufzeit der Option muss zwischen 1 und 9 liegen
        if (!(laufZeitOption > 1 || laufZeitOption < 10)) {
            showError("Die Laufzeit musss zwischen 1 und 9 liegen.");
            return;
        }



        Double deltat = 0d;

        int j = this.jComboBox3.getSelectedIndex();
        mode selectedMode;
        if (j == 0) {
            //Wöchentlich
            deltat = 1d / 52d;
            selectedMode = mode.WEEK;
        } else if (j == 1) {
            //Monatlich
            deltat = 1d / 12d;
            selectedMode = mode.MONTH;
        } else {
            //Jährlich
            deltat = 1d;
            selectedMode = mode.YEAR;
        }


        //Hole das gewählte Referenzdatum und lade die entsprechenden Zinswerte aus der Datei
        int referenzDatum = this.jComboBox2.getSelectedIndex();
        RateInputReader reader = new RateInputReader(this.RATE_INPUT);
        List zinsListe = null;

        try {
            zinsListe = reader.getRates(referenzDatum, j);
        } catch (Exception e) {
            showError("Zinsdaten für das Referenzdatum " + referenzDatum + " konnten nicht gelesen werden.");
            return;
        }




        //Welche Berechnungsart wurde selektiert
        int i = this.jComboBox1.getSelectedIndex();
        //Option
        if (i == 0 || i == 1) {

            this.jLabel6.setText("Optionswert");

            //Wurde eine Laufzeit für den Bond eingegeben
            Integer laufZeitBond = 0;
            if ((laufZeitBond = isInteger(this.jTextField4.getText())) == null) {
                showError("Bitte geben Sie eine Laufzeit für den Bond an (nur ganze Zahlen).");
                return;
            }

            //Die Laufzeit des Bonds muss länger sein als die Laufzeit der Option
            if (laufZeitOption >= laufZeitBond) {
                showError("Die Laufzeit der Option muss kleiner als die Laufzeit des Bonds sein.");
                return;
            }

            //Die Laufzeit des Bonds kann max 10 sein
            if (laufZeitBond > 10) {
                showError("Die Laufzeit des Bonds darf nicht größer als 10 sein.");
                return;
            }

            Double nominal = 0d;
            if ((nominal = isDouble(this.jTextField2.getText())) == null) {
                showError("Bitte geben Sie ein Nominal für den Bond an.");
                return;
            }

            Double strike = 0d;
            if ((strike = RateInputReader.getDouble(this.jTextField3.getText())) == null) {
                showError("Bitte geben Sie ein Strike für den Bond an.");
                return;
            }




            tree = new TrinomialTree(zinsListe, deltat, selectedMode);



            if (i == 0) {
                result = tree.calculateEuropeanPut(laufZeitOption, laufZeitBond, strike, nominal);
                tree.printTree(i);
            } else {
                result = tree.calculateEuropeanCall(laufZeitOption, laufZeitBond, strike, nominal);
                tree.printTree(i);
            }

            this.showInfo("Berechnung abgeschlossen.");

        } //Swaption
        else if (i == 2 || i == 3) {


            this.jLabel6.setText("PayOff-Swaption");

            //Laufzeit des Swaps - default auf 1
            Integer laufZeitSwap = 1;

            //Häufigkeit der Cash Settlements
            Integer haeufigkeitSettlements = Integer.valueOf((String)this.jComboBox4.getSelectedItem());



            //Hole das gewählte Referenzdatum und lade die entsprechenden Zinswerte aus der Datei

            List swapRates = null;
            try {
                swapRates = reader.getSwapRates(referenzDatum, haeufigkeitSettlements, laufZeitOption);
            } catch (Exception e) {
                showError("Swaprates konnten nicht gelesen werden.");
                return;
            }


            //Fixed Rate
            Double fixedRate = 0d;
            if ((fixedRate = isDouble(this.jTextField7.getText().replaceAll(",", "."))) == null) {
                showError("Bitte geben Sie eine Fixed Rate an.");
                return;
            }

            //Der User gibt die Fixed-Rate in ganzen Prozent an
            fixedRate = fixedRate/100;

            tree = new TrinomialTree(zinsListe, deltat,selectedMode);


            //Put Swaption
            if (i == 2) {
                try {
                    result = tree.calculatePayerSwaption(laufZeitOption, laufZeitSwap, haeufigkeitSettlements, fixedRate, swapRates);
                    tree.printTree(i);
                    this.showInfo("Berechnung abgeschlossen.");
                }
                catch (PayOffNegativeException e) {
                    showError("Fixed rate größer SWAP-Rate. Option verfallen lassen.");
                }


            } //Call Swaption
            else {
                try {
                    result = tree.calculateReceiverSwaption(laufZeitOption, laufZeitSwap, haeufigkeitSettlements, fixedRate, swapRates);
                    tree.printTree(i);
                    this.showInfo("Berechnung abgeschlossen.");
                }
                catch (PayOffNegativeException e) {
                    showError("SWAP Rate größer Fixed rate. Option verfallen lassen.");
                }

            }





        }


        this.jLabel7.setText(String.valueOf(result));


        
    }//GEN-LAST:event_actionPerformed

    private Integer isInteger(String s) {

        try {
            return new Integer(s);
        } catch (Exception e) {
            return null;
        }

    }

    private Double isDouble(String s) {

        try {
            return new Double(s);
        } catch (Exception e) {
            return null;
        }

    }

    private void itemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_itemStateChanged

        

        //Welche Berechnungsart wurde selektiert
        int i = this.jComboBox1.getSelectedIndex();
        switch (i) {
            //European Put
            case 0: {
                this.jTabbedPane1.setSelectedIndex(0);

            }
            ;
            break;
            //European Call
            case 1: {
                this.jTabbedPane1.setSelectedIndex(0);

            }
            ;
            break;
            //European Put Swaption
            case 2: {
                this.jTabbedPane1.setSelectedIndex(1);

            }
            ;
            break;
            //European Call Swaption
            case 3: {
                this.jTabbedPane1.setSelectedIndex(1);

            }
            ;
            break;

        }


    }//GEN-LAST:event_itemStateChanged

    private void itemStateChangedDatum(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_itemStateChangedDatum
        //Welches Datum hat der User ausgewählt?
    }//GEN-LAST:event_itemStateChangedDatum

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       
         java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                //Lösche alle für die Zeichnung nicht relevanten Knoten
                tree.deleteNodesWithoutPredecessors();

                visualizer = new Visualizer(tree);
                visualizer.setVisible(true);
            }
        });
       
       
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private JDialog aboutBox;
}
